/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 18, 2012  12:15:22 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.rental.invoice
 * File Name : InvoiceProcessing.java
 * TODO
 */
package apps.business.process.rental.invoice;

import java.util.List;

import jpa.entitys.synchro.staging.FiArinvrentOutb;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import sap.business.fi.outbound.FiArinvrentOutbService;

import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.VwSyncInvRentalAction;

/**
 * @author chatree
 *
 */
public class InvoiceProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(InvoiceProcessing.class);

	/**
	 * 
	 */
	public InvoiceProcessing() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * @param company
	 * @param project
	 * @param contractRefNo
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 * String company = "SP";
	 * String project = "SPL_GRAND"; 
	 * String contractRefNo = null;
	 * String documentNo = "LI-1205-000010";
	 * String status = "A";
	 * String syncIsstaging = "N";
	 */
	public void Processing(String company, String project, String contractRefNo, String documentNo, String status, String syncIsstaging) throws Exception {		
		try{
			/*
			log.info("company = " + CommonService.IsNullRString(company));
			log.info("project = " + CommonService.IsNullRString(project));
			log.info("contractRefNo = " + CommonService.IsNullRString(contractRefNo));
			log.info("documentNo = " + CommonService.IsNullRString(company));
			log.info("status " + CommonService.IsNullRString(status));
			log.info("syncIsstaging = " + CommonService.IsNullRString(syncIsstaging));
			*/
			
			
			//Create To Staging
			UPD_SENDUseTime(true, false, null);
			List<FiArinvrentOutb> value_list = new VwSyncInvRentalAction().CreateToStaging(company, project, contractRefNo, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			System.out.println("Created To Staging  size=" + CommonService.getSize(value_list));
			
			//Send to SAP
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new FiArinvrentOutbService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			
			System.out.println("Sended To SAP  size=" + CommonService.getSize(value_list));
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void SendingAllWithNonSending() throws Exception {
		try{
			List<Object[]> lst = new VwSyncInvRentalAction().SearchAllNonSending();
			
			System.out.println("SearchAllNonSending size=" + CommonService.getSize(lst));
			
			if(!CommonService.IsNull(lst)){
				for(Object[] value : lst){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String contractRefNo = null;
					String documentNo = value[2].toString();
					String status = value[3].toString();
					String syncIsstaging = value[4].toString();
					
					System.out.println("SendingAllWithNonSending company=" + company + " || project=" + project + " || contractRefNo=" + contractRefNo + " || documentNo=" + documentNo + " || status=" +status + " || syncIsstaging=" + syncIsstaging);
					log.info("SendingAllWithNonSending company=" + company + " || project=" + project + " || contractRefNo=" + contractRefNo + " || documentNo=" + documentNo + " || status=" +status + " || syncIsstaging=" + syncIsstaging);
					
					//Create To Staging
					List<FiArinvrentOutb> list = new VwSyncInvRentalAction().CreateToStaging(company, project, contractRefNo, documentNo, status, syncIsstaging);
					
					//Send to SAP
					new FiArinvrentOutbService().SendToSAP(list);
					System.out.println("Sending to SAP size=" + CommonService.getSize(list));
					log.info("Sending to SAP size=" + CommonService.getSize(list));
					
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	private void Start() throws Exception{
		try{
			
			//IV-1203-000001 - fee pers 
			
			//IV-1205-000006 INVOICE VAT+WHT
			//IV-1205-000004 INVOICE VAT
			//IV-1205-000003 INVOICE NOVAT
			
			/*
			Charge code RT011
			CUSTOMER : 20000001
			INVOICE : LI/1205/000002
			receipt : LP/1205/000006
			
			Charge code RT011
			CUSTOMER : 10000086
			INVOICE : LI/1205/000006
			receipt : LP/1205/000007
			*/
			
			/*
			 * 
			 * 	Charge code RT024
				CUSTOMER : 20000001
				INVOICE LI/1205/000007
				receipt LP/1205/000008;
			 */
			
			VwSyncInvRentalAction objj = new VwSyncInvRentalAction();
			String company = "SP";
			String project = "SPL_GRAND"; 
			String contractRefNo = null;
			String documentNo = "LI-1205-000010";
			String status = "A";
			String syncIsstaging = "N";
			
			//Create To Staging
			List<FiArinvrentOutb> value_list = objj.CreateToStaging(company, project, contractRefNo, documentNo, status, syncIsstaging);
			
			//Send to SAP
			new FiArinvrentOutbService().SendToSAP(value_list);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		try{
			
			new InvoiceProcessing().SendingAllWithNonSending();
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
}

